package com.sykuku.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sykuku.common.R;
import com.sykuku.controller.expection.BusinessException;
import com.sykuku.pojo.Category;
import com.sykuku.dao.CategoryMapper;
import com.sykuku.pojo.Dish;
import com.sykuku.pojo.Setmeal;
import com.sykuku.service.CategoryService;
import com.sykuku.service.DishService;
import com.sykuku.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
//继承Iservice的实现接口类就可以直接获取Iservice中的方法  再次实现定的service接口
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService   {

    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;
    // 删除
    @Override
    public void deleteByID(Long id) {
        LambdaQueryWrapper<Dish> dish = new LambdaQueryWrapper<>();
        dish.eq(Dish::getCategoryId,id);
        int count = dishService.count(dish);
        if (count>0){

            log.error("阻止用户删除关联分类");
            throw new BusinessException(1,"此分类中还有菜品");
        }

        LambdaQueryWrapper<Setmeal> setmeal = new LambdaQueryWrapper<>();
        setmeal.eq(Setmeal::getCategoryId,id);
        int count1 = setmealService.count(setmeal);

        if (count1>0){
            log.error("阻止用户删除关联分类");
            throw new BusinessException(1,"此分类中还有套餐");

        }
        removeById(id);
    }

    @Override
    public List<Category> CategoryType(Integer type) {
        log.debug("在业务层处理数据用户的回显菜品分类{}");
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Category::getType,type);

        return list(lqw);

    }

}
